package com.erp.gather.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.gather.domain.bo.ErpSellerProductExpandBo;
import com.erp.gather.domain.vo.ErpSellerProductExpandVo;
import com.erp.gather.mapper.ErpSellerProductExpandMapper;
import com.erp.gather.service.IErpSellerProductExpandService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.gather.api.domain.ErpSellerProductExpand;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * ERP个人商品拓展;参考AE-商品：发布产品信息（aliexpress.postproduct.redefining.postaeproduct）Service业务层处理
 *
 * @author erp
 * @date 2023-03-28
 */
@RequiredArgsConstructor
@Service
public class ErpSellerProductExpandServiceImpl implements IErpSellerProductExpandService {

    private final ErpSellerProductExpandMapper baseMapper;

    /**
     * 查询ERP个人商品拓展;参考AE-商品：发布产品信息（aliexpress.postproduct.redefining.postaeproduct）
     */
    @Override
    public ErpSellerProductExpandVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询ERP个人商品拓展;参考AE-商品：发布产品信息（aliexpress.postproduct.redefining.postaeproduct）列表
     */
    @Override
    public TableDataInfo<ErpSellerProductExpandVo> queryPageList(ErpSellerProductExpandBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpSellerProductExpand> lqw = buildQueryWrapper(bo);
        Page<ErpSellerProductExpandVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询ERP个人商品拓展;参考AE-商品：发布产品信息（aliexpress.postproduct.redefining.postaeproduct）列表
     */
    @Override
    public List<ErpSellerProductExpandVo> queryList(ErpSellerProductExpandBo bo) {
        LambdaQueryWrapper<ErpSellerProductExpand> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpSellerProductExpand> buildQueryWrapper(ErpSellerProductExpandBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpSellerProductExpand> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSellerProductId() != null, ErpSellerProductExpand::getSellerProductId, bo.getSellerProductId());
        lqw.eq(StringUtils.isNotBlank(bo.getSkuProperty()), ErpSellerProductExpand::getSkuProperty, bo.getSkuProperty());
        lqw.eq(StringUtils.isNotBlank(bo.getAbsoluteQuoteMap()), ErpSellerProductExpand::getAbsoluteQuoteMap, bo.getAbsoluteQuoteMap());
        lqw.eq(StringUtils.isNotBlank(bo.getIsPackSell()), ErpSellerProductExpand::getIsPackSell, bo.getIsPackSell());
        lqw.eq(StringUtils.isNotBlank(bo.getBaseUnit()), ErpSellerProductExpand::getBaseUnit, bo.getBaseUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getAddUnit()), ErpSellerProductExpand::getAddUnit, bo.getAddUnit());
        lqw.eq(bo.getAddWeight() != null, ErpSellerProductExpand::getAddWeight, bo.getAddWeight());
        lqw.eq(bo.getBulkDiscount() != null, ErpSellerProductExpand::getBulkDiscount, bo.getBulkDiscount());
        lqw.eq(bo.getBulkOrder() != null, ErpSellerProductExpand::getBulkOrder, bo.getBulkOrder());
        lqw.eq(StringUtils.isNotBlank(bo.getCurrencyCode()), ErpSellerProductExpand::getCurrencyCode, bo.getCurrencyCode());
        lqw.eq(bo.getDeliveryTime() != null, ErpSellerProductExpand::getDeliveryTime, bo.getDeliveryTime());
        lqw.eq(StringUtils.isNotBlank(bo.getIsImageDynamic()), ErpSellerProductExpand::getIsImageDynamic, bo.getIsImageDynamic());
        lqw.eq(StringUtils.isNotBlank(bo.getKeyword()), ErpSellerProductExpand::getKeyword, bo.getKeyword());
        lqw.eq(StringUtils.isNotBlank(bo.getProductMoreKeywords1()), ErpSellerProductExpand::getProductMoreKeywords1, bo.getProductMoreKeywords1());
        lqw.eq(StringUtils.isNotBlank(bo.getProductMoreKeywords2()), ErpSellerProductExpand::getProductMoreKeywords2, bo.getProductMoreKeywords2());
        lqw.eq(bo.getLotNum() != null, ErpSellerProductExpand::getLotNum, bo.getLotNum());
        lqw.eq(StringUtils.isNotBlank(bo.getOwnerMemberId()), ErpSellerProductExpand::getOwnerMemberId, bo.getOwnerMemberId());
        lqw.eq(bo.getOwnerMemberSeq() != null, ErpSellerProductExpand::getOwnerMemberSeq, bo.getOwnerMemberSeq());
        lqw.eq(StringUtils.isNotBlank(bo.getPackageType()), ErpSellerProductExpand::getPackageType, bo.getPackageType());
        lqw.eq(bo.getStock() != null, ErpSellerProductExpand::getStock, bo.getStock());
        lqw.eq(StringUtils.isNotBlank(bo.getClassifyLabel()), ErpSellerProductExpand::getClassifyLabel, bo.getClassifyLabel());
        lqw.eq(StringUtils.isNotBlank(bo.getSummary()), ErpSellerProductExpand::getSummary, bo.getSummary());
        lqw.eq(bo.getProductStatus() != null, ErpSellerProductExpand::getProductStatus, bo.getProductStatus());
        lqw.eq(bo.getEditStatus() != null, ErpSellerProductExpand::getEditStatus, bo.getEditStatus());
        lqw.eq(bo.getBrandId() != null, ErpSellerProductExpand::getBrandId, bo.getBrandId());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpSellerProductExpand::getRevision, bo.getRevision());
        return lqw;
    }

    /**
     * 新增ERP个人商品拓展;参考AE-商品：发布产品信息（aliexpress.postproduct.redefining.postaeproduct）
     */
    @Override
    public Boolean insertByBo(ErpSellerProductExpandBo bo) {
        ErpSellerProductExpand add = BeanUtil.toBean(bo, ErpSellerProductExpand.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改ERP个人商品拓展;参考AE-商品：发布产品信息（aliexpress.postproduct.redefining.postaeproduct）
     */
    @Override
    public Boolean updateByBo(ErpSellerProductExpandBo bo) {
        ErpSellerProductExpand update = BeanUtil.toBean(bo, ErpSellerProductExpand.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpSellerProductExpand entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除ERP个人商品拓展;参考AE-商品：发布产品信息（aliexpress.postproduct.redefining.postaeproduct）
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public R<Object> getSellerProductExpandInfo(ErpSellerProductExpand erpSellerProductExpand) {
        ErpSellerProductExpandBo bo = BeanUtil.copyProperties(erpSellerProductExpand, ErpSellerProductExpandBo.class);
        ErpSellerProductExpand expand = baseMapper.selectOne(buildQueryWrapper(bo));
        if (Objects.isNull(expand)) {
            return R.ok(null);
        } else {
            return R.ok(expand);
        }
    }
}
